package com.world.cat.service.generate;

import com.world.cat.model.generate.Auto;
import com.world.common.generate.freemaker.util.StrUtils;
import com.world.common.generate.freemaker.util.file.FileUtils;
import com.world.common.generate.freemaker.util.fremarker.FreemarkerUtil;
import com.world.common.generate.freemaker.util.fremarker.ModelTemplate;
import com.world.common.util.DateUtil;
import com.world.common.util.SysUtil;

import java.io.File;
import java.util.Date;

/**
 * V1.0
 * 生成模版代码
 */
public class CodeUtils {
    private static String outFolder = "D:/freemaker/genCode";

    public static void main(String arges[]) throws Exception {
        genCode();
//        genTest();
    }

    public static void genCode() {
        generate(Auto.class, CodeType.FILE);
//        generate(MyAuths.class, CodeType.FRAGMENT);
    }

    public static void generate(Class clazz, CodeType codeEnums) {
        try {
            StackTraceElement[] s = new Exception().getStackTrace();
            String className = s[1].getClassName();
            FileUtils.deleteAllFile(new File(outFolder), false);
            FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
            String ftlFolder = "";
            switch (codeEnums) {
                case FILE:
                    ftlFolder = ClassLoader.getSystemResource("").getPath() + "generate";
                    break;
                case FRAGMENT:
                    ftlFolder = ClassLoader.getSystemResource("").getPath() + "generate/fragment";
                    break;
            }

            if (!CodeUtils.class.getSimpleName().equals(className)) {
                ftlFolder = ftlFolder.replace("test-classes", "classes");
            }
            ftlFolder = ftlFolder.replaceAll("%20", " ");
            freemarkerUtil.setEn(ftlFolder);
            //目录有空格路径替换
            File folder = new File(ftlFolder);
            ModelTemplate modelTemplate = initModelTemplate(clazz);
            if (folder.exists()) {
                switch (codeEnums) {
                    case FILE:
                        genFile(freemarkerUtil, modelTemplate, folder.listFiles());
                        break;
                    case FRAGMENT:
                        genFragment(freemarkerUtil, modelTemplate, folder.listFiles());
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void genFile(FreemarkerUtil freemarkerUtil, ModelTemplate modelTemplate, File[] files) throws Exception {
        outFolder += DateUtil.format("yyyyMMddHHmmss", new Date());
        for (File f : files) {
            if (f.isDirectory()) {
                continue;
            }
            String fileName = f.getName().substring(0, f.getName().lastIndexOf("."));
            String destPath = outFolder + File.separator + fileName.toLowerCase() + File.separator + "back" + File.separator + modelTemplate.getClassName() + fileName + ".java";
            if (fileName.startsWith("Page")) {
                destPath = outFolder + File.separator + "_jsp" + File.separator + "back" + File.separator +
                        SysUtil.lowFirst(modelTemplate.getClassName()) + File.separator
                        + SysUtil.lowFirst(fileName.replace("Page", "")) + ".jsp";
            }
            freemarkerUtil.process(f.getName(), destPath, modelTemplate);
        }
        System.out.println("生成成功");
        System.out.println("output folder: " + outFolder);
    }

    private static void genFragment(FreemarkerUtil freemarkerUtil, ModelTemplate modelTemplate, File[] files) throws Exception {
        for (File f : files) {
            String str = freemarkerUtil.getFileString(f.getName(), modelTemplate);
            System.out.println(str);
        }
    }

    public static void genOne(ModelTemplate modelTemplate, String templateName) throws Exception {
        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
        String ftlFolder = ClassLoader.getSystemResource("").getPath() + "generate";
        ftlFolder = ftlFolder.replaceAll("%20", " ");
        freemarkerUtil.setEn(ftlFolder);
        String destPath = outFolder + File.separator + "test" + File.separator + modelTemplate.getClassName() + "test" + ".java";
        freemarkerUtil.process(templateName, destPath, modelTemplate);
    }

    public static ModelTemplate initModelTemplate(Class clazz) {
        ModelTemplate modelTemplate = new ModelTemplate();
        int index = clazz.getName().indexOf(".model");
        if (index == -1) {
            System.out.println("can't get the namespace");
        }
        String namespace = clazz.getName().substring(0, index);
        modelTemplate.setNamespace(namespace);
        modelTemplate.setFullClassName(clazz.getName());
        modelTemplate.setClassName(clazz.getSimpleName());
        modelTemplate.setClassNameLowerCase(StrUtils.toLowerCaseFirstOne(clazz.getSimpleName()));
        modelTemplate.setFieldMap(FieldService.getFieldMap(clazz));
        return modelTemplate;
    }

}
